package com.das.mechanic_base.mvp.view.create;


import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.core.content.ContextCompat;

import com.das.mechanic_base.R;
import com.das.mechanic_base.R2;
import com.das.mechanic_base.base.X3BaseActivity;
import com.das.mechanic_base.bean.main.DetectionNewBean;
import com.das.mechanic_base.mvp.contract.create.X3AloneSignContract;
import com.das.mechanic_base.mvp.presenter.create.X3AloneSignPresenter;
import com.das.mechanic_base.utils.ImageDataUtils;
import com.das.mechanic_base.utils.X3FileUtils;
import com.das.mechanic_base.utils.X3StatusBarUtil;
import com.das.mechanic_base.utils.X3ToastUtils;
import com.das.mechanic_base.widget.PathPointView;
import com.das.mechanic_base.widget.Watermark;
import com.das.mechanic_base.widget.X3AloneCommitSignDialog;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class X3ServiceSignActivity extends X3BaseActivity<X3AloneSignPresenter> implements X3AloneSignContract.View, X3AloneCommitSignDialog.IBtnClick {

    @BindView(R2.id.tv_title)
    TextView tv_title;
    @BindView(R2.id.ppv_sign)
    PathPointView ppv_sign;
    @BindView(R2.id.iv_show)
    ImageView iv_show;
    @BindView(R2.id.tv_save)
    TextView tv_save;
    @BindView(R2.id.fl_sign)
    FrameLayout fl_sign;
    @BindView(R2.id.ll_sign)
    LinearLayout ll_sign;
    long receiveBaseId;

    boolean canSave;
    String storeName = "";

    @Override
    protected int getLayoutId() {
        return R.layout.activity_x3_sign;
    }

    @Override
    protected void initView() {
        setSwipeBackEnable(false);
        X3StatusBarUtil.darkMode(this);
        receiveBaseId = getIntent().getLongExtra("receiveBaseId", 0L);
        storeName = getIntent().getStringExtra("storeName");
        ppv_sign.setOnToucheDraw(new PathPointView.OnToucheDraw() {
            @Override
            public void onToucheDraw() {
                canSave = true;
                tv_save.setTextColor(ContextCompat.getColor(X3ServiceSignActivity.this,R.color.white));
                tv_save.setBackgroundResource(R.drawable.x3_alone_sign_save_bg);
            }
        });
        ll_sign.setBackground(Watermark.getInstance().getDrawable(storeName, Color.parseColor("#EBE9ED"),18));
    }

    @Override
    protected X3AloneSignPresenter createPresenter() {
        return new X3AloneSignPresenter();
    }

    @OnClick({R2.id.iv_back, R2.id.tv_reset, R2.id.tv_save})
    public void onViewClick(View view) {
        if (view.getId() == R.id.iv_back) {
            finish();
        } else if (view.getId() == R.id.iv_add) {

        } else if (view.getId() == R.id.tv_reset) {
            ppv_sign.reset();
            canSave = false;
            tv_save.setTextColor(ContextCompat.getColor(X3ServiceSignActivity.this,R.color.white));
            tv_save.setBackgroundResource(R.drawable.x3_alone_sign_save_grey_bg);
        } else if (view.getId() == R.id.tv_save) {
            if (!canSave){
                return;
            }
            if (ppv_sign.modify){
                showTips();
            } else {
                X3ToastUtils.showMessage(getString(R.string.x3_alone_no_sign));
            }
        }
    }

    private void showTips(){
        X3AloneCommitSignDialog dialog  = new X3AloneCommitSignDialog(this);
        dialog.setiBtnClick(this);
        dialog.show();
    }

    @Override
    public void upLoadSuccess(long picId) {
        Log.e("picFile--", "picId = " + picId);
        DetectionNewBean newBean = new DetectionNewBean();
        newBean.sign = picId;
        newBean.questionToSystemSn = "sys_sign_001";
        List<DetectionNewBean> mList = new ArrayList<>();
        mList.add(newBean);
        mPresenter.saveDetectionFirstInfo(mList, receiveBaseId);
    }

    @Override
    public void upLoadFail() {
        hideLoading();
    }

    @Override
    public Context getContext() {
        return this;
    }

    @Override
    public void upUpdateSuccess() {
        hideLoading();
        Intent mIntent = new Intent();
        setResult(RESULT_OK, mIntent);
        finish();
    }

    @Override
    public void upUpdateFail(String picId) {
        hideLoading();
    }

    @Override
    public void iBtnAffirmClick() {

        showLoading("");
        Bitmap bitmap = ImageDataUtils.getViewBitmap(fl_sign);
//        Bitmap bitmap = getBitmapFromView(ppv_sign);

        String fileDir = X3FileUtils.getStoragePath(this, X3FileUtils.MECHANIC_PHOTO_STORAGE_PATH) + "work_alone" ;
        File imgFile = new File(fileDir);
        if (!imgFile.getParentFile().exists()) {
            imgFile.getParentFile().mkdirs();
        }
        if (!imgFile.exists()) {
            try {
                imgFile.mkdirs();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        FileOutputStream fos = null;
        try {
            File picFile  = File.createTempFile( "sign",".jpg",imgFile);
            fos = new FileOutputStream(picFile);
            Bitmap newBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
            Canvas canvas = new Canvas(newBitmap);
            canvas.drawColor(Color.WHITE);
            canvas.drawBitmap(bitmap, 0, 0, null);
            newBitmap.compress(Bitmap.CompressFormat.JPEG, 100,fos);
//            bitmap.compress(Bitmap.CompressFormat.JPEG, 100,fos);
            mPresenter.uploadImageAws(picFile);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (fos!=null){
                try {
                    fos.flush();
                    fos.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static Bitmap getBitmapFromView(View v) {
        Bitmap b = Bitmap.createBitmap(v.getWidth(), v.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas c = new Canvas(b);
        v.layout(v.getLeft(), v.getTop(), v.getRight(), v.getBottom());
        // Draw background
        Drawable bgDrawable = v.getBackground();
        if (bgDrawable != null)
            bgDrawable.draw(c);
        else
            c.drawColor(Color.WHITE);
        // Draw view to canvas
        v.draw(c);
        return b;
    }
}